<?php
namespace Admin\Model;
use Think\Model;
class GoodsModel extends Model {
    public function search()
    {
        /*************搜索*********************/
        $keyword = I('get.keyword');
        $where = array();

        if($keyword)
        {   
            $where['product_name'] = array('like','%'.$keyword.'%');
        }
        $count = $this->count();
        $data = $this->where($where)->order('id asc')->select();
        /*************翻页*******************/
        // $count = $this->count();
        // $pageObj = new \Think\Page($count,$perPage);
        // $pageString = $pageObj->show();

        // $data = $this->where($where)
        //         ->order('id')
        //         ->limit($pageObj->firstRow.','.$pageObj->listRows)
        //         ->select();
        
        //var_dump($data);die;
        
        return array(
            'goods'     =>  $data,
            'total'     =>  $count,
            );
    }
    public function cate()
    {
        $data = M('Good_cates')->select();
        $list = array();
        $i = 0;
        foreach ($data as $val) {
            $list[$i]['id'] = $val['id'];
            $list[$i]['pId'] = $val['parent_id'];
            $list[$i]['name'] = $val['cate_name'];
            $i+=1;
        }

        return json_encode($list);
    }
    public function region()
    {
        $data = M('Good_region')->select();
        $list = array();
        $i = 0;
        foreach ($data as $val) {
            $list[$i]['id'] = $val['id'];
            $list[$i]['pId'] = 0;
            $list[$i]['name'] = $val['region_name'];
            $i+=1;
        }

        return json_encode($list);
    }
    public function brand()
    {
        $data = M('Good_brand')->select();
        $list = array();
        $i = 0;
        foreach ($data as $val) {
            $list[$i]['id'] = $val['id'];
            $list[$i]['pId'] = 0;
            $list[$i]['name'] = $val['brand_name'];
            $i+=1;
        }

        return json_encode($list);
    }
    public function getTreeList()
    {
        $data = M('Good_cates')->select();
        //var_dump($data);die;
        $list = $this->getTree($data,0,0);
        return $list;

    }
    public function getTree($list,$p_id=0,$deep=0)
    {
        static $tree = array();
        foreach($list as $row){
            if($row['parent_id'] == $p_id){
                $row['deep'] = $deep;
                $tree[] = $row;
                //var_dump($list);die;
                $this->getTree($list,$row['id'],$deep+1);
            }
        }
        //var_dump($tree);die;
        return $tree;
    }
}